// Task 处理异步任务
const fs = require('fs');
const { task } = require('folktale/concurrency/task');
const { split, find } = require('lodash/fp');

function readFile(filename) {
  return task((resolver) => {
    fs.readFile(filename, 'utf-8', (err, data) => {
      if (err) resolver.reject(err);
      resolver.resolve(data);
    });
  });
}

readFile('package.json')
  .map(split('\n'))
  .map(find((x) => x.includes('version')))
  // task函子的 run方法 执行异步的函数
  .run()
  // 监听执行的状态
  .listen({
    // 失败事件监听
    onRejected: (err) => {
      console.log(err);
    },
    // 成功事件监听
    onResolved: (value) => {
      console.log(value);
    },
  });
